import Api from "../../api"

const state = {
    auth: {},
    flag: false
}

const mutations = {
    setAuth(state, auth) {
        state.auth = auth
    },
    setFlag(state, flag) {
        state.flag = flag
    }
}

const actions = {
    async Login({ commit }, user) {
        let { data } = await Api.Core.login(user);
        commit('setAuth', data)
        localStorage.setItem('token', data.data.token)
        localStorage.setItem('AuthKey', data.data.key)
    },
    async Register({ commit }, user) {
        try {
            await Api.Core.register(user)
            commit('setFlag',true)
        } catch (error) {
            commit('setFlag',false)
        } 
    }
}

const getters = {
    isLogin(state) {
        return state.auth
    },
    getFlag(state){
        return state.flag
    },
}

export default {
    state, mutations, actions, getters
}